﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IntelligentQuery.Model;

namespace IntelligentQuery.TransformProviders
{
    public class LikeTransformProvider : ITransformProvider
    {
        public bool Match(ConditionItem item, Type type)
        {
            return item.Method == QueryMethod.Like;
        }

        public IEnumerable<ConditionItem> Transform(ConditionItem item, Type type)
        {
            var str = item.Value.ToString();
            //var keyWords = str.Split('*');
            //if (keyWords.Length == 1)
            //{
            //    return new[] { new ConditionItem { Field = item.Field, Method = QueryMethod.Equal, Value = item.Value } };
            //}
            var list = new List<ConditionItem>();
            //if (!string.IsNullOrEmpty(keyWords.First()))
            //    list.Add(new ConditionItem { Field = item.Field, Method = QueryMethod.StartsWith, Value = keyWords.First() });
            //if (!string.IsNullOrEmpty(keyWords.Last()))
            //    list.Add(new ConditionItem { Field = item.Field, Method = QueryMethod.EndsWith, Value = keyWords.Last() });
            //for (int i = 1; i < keyWords.Length - 1; i++)
            //{
            //    if (!string.IsNullOrEmpty(keyWords[i]))
            //        list.Add(new ConditionItem { Field = item.Field, Method = QueryMethod.Contains, Value = keyWords[i] });
            //}
            list.Add(new ConditionItem { Field = item.Field, Method = QueryMethod.Contains, Value = str });
            return list;
        }
    }
}
